﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace SAHCPIMS_final
{
    public partial class ADDFAMILY : Form
    {
        public string homeEmpName;
        public int homeEmpNum;

        public ADDFAMILY()
        {
            InitializeComponent();
        }

        private void ADDFAMILY_Load(object sender, EventArgs e)
        {
            empNumLabel.Text = homeEmpNum.ToString();
            employeeLabel.Text = homeEmpName;
        }

        private void addfamilybutton_Click(object sender, EventArgs e)
        {
            ADDFAMILY addFamilyForm = new ADDFAMILY();
            addFamilyForm.homeEmpName = homeEmpName;
            addFamilyForm.homeEmpNum = homeEmpNum;
            addFamilyForm.Visible = true;
            this.Visible = false;
        }
        
        private void searchbutton_Click(object sender, EventArgs e)
        {
            SEARCH searchForm = new SEARCH();
            searchForm.homeEmpName = homeEmpName;
            searchForm.homeEmpNum = homeEmpNum;
            searchForm.Visible = true;
            this.Visible = false;
        }

        private void logOutButton_Click(object sender, EventArgs e)
        {
            LOGIN logout = new LOGIN();
            logout.Visible = true;
            this.Visible = false;
        }

        private void homebutton_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.homeEmpName = homeEmpName;
            home.homeEmpNum = homeEmpNum;
            home.Visible = true;
            this.Visible = false;
        }

        private void Submit_Click(object sender, EventArgs e)
        {
            int doFlag = 0;
            string match = "";

            //Storage of Form Values
            string fname = Firstname.Text;
            string mname = Middlename.Text;
            string lname = Lastname.Text;
            string bdate = bdaytextfield.Text;
            string address = Address.Text;

            //Match Values to Valid inputs
            Match fnameMatch = System.Text.RegularExpressions.Regex.Match(fname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match mnameMatch = System.Text.RegularExpressions.Regex.Match(mname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match lnameMatch = System.Text.RegularExpressions.Regex.Match(lname, @"^[A-Z][a-zA-Z]*([' 'A-Z][a-zA-Z]*)*$");
            Match addMatch = System.Text.RegularExpressions.Regex.Match(address, @"[0-9a-zA-Z]+$");
            Match bday = System.Text.RegularExpressions.Regex.Match(bdate, @"^((0[1-9])|10|11|12)/(([0-2][1-9])|10|20|30|31])/(19|20)[0-9][0-9]$");

            if (!fnameMatch.Success) MessageBox.Show("Invalid Firstname Input");
            else if (!mnameMatch.Success) MessageBox.Show("Invalid Middlename Input");
            else if (!lnameMatch.Success) MessageBox.Show("Invalid Lastname Input");
            else if (!bday.Success) MessageBox.Show("Invalid Birthdate Input");
            /** =========== Editted by: MONICA TOSINO :: Date Checking :: March 2, 2010 =========== **/
            else if ((bdate[1] == '2' && bdate.Substring(3, 2) == "29" && (int.Parse(bdate.Substring(6, 4)) % 4) != 0)
                    || ((bdate[1] == '4' || bdate[1] == '6' || bdate[1] == '9' || bdate.Substring(0, 2) == "11") && bdate.Substring(3, 2) == "31"))
            {
                MessageBox.Show("Incorrect Birthdate");
            }
            /** =========================== End of Update ======================== **/
            else if (!addMatch.Success) MessageBox.Show("Invalid Address Input");
            else
            {

                string conString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\SAHCPIMS_database.mdb";

                OleDbConnection conn = new OleDbConnection(conString);

                DataSet ds = new DataSet();

                try
                {
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Family", conn);
                    adapter.Fill(ds);
                }
                catch (OleDbException)
                {
                    MessageBox.Show("Error in retrieving values");
                }
                conn.Close();

                DataTable dt = ds.Tables[0];
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["FirstName"].ToString() == fname && dr["LastName"].ToString() == lname)
                    {
                        if (dr["MiddleName"].ToString() == mname)
                        {
                            match = "Name";
                            if (dr["Address"].ToString() == address)
                                match = match + "and Address";
                        }
                    }
                   // else if (dr["Address"].ToString() == address) match = "Address";
                }

                if (match != "")
                {
                    MessageBox.Show("Error! " + match + " Already in the Records");
                }
                else
                {
                    OleDbConnection conn2 = new OleDbConnection(conString);
                    DataSet dd = new DataSet();

                    try
                    {
                        conn2.Open();
                        OleDbDataAdapter adapter2 = new OleDbDataAdapter("INSERT into Family (FirstName, MiddleName, LastName, Birthday, Address) values('" + fname + "', '" + mname + "', '" + lname + "', '" + bdate + "', '" + address + "')", conn2);
                        adapter2.Fill(dd);
                        doFlag = 1;
                    }
                    catch (OleDbException)
                    {
                        MessageBox.Show("Error in inserting values");
                    }
                    conn2.Close();

                    if (doFlag == 1)
                    {
                        nameresult.Text = fname + " " + mname + " " + lname;
                        bdayresult.Text = bdate;
                        addressresult.Text = address;
                        conn.Open();
                        OleDbDataAdapter adapter = new OleDbDataAdapter("Select * from Family where FirstName='" + fname + "' and MiddleName='" + mname + "' and LastName='" + lname + "'", conn);
                        adapter.Fill(ds);
                        conn.Close();
                        dt = ds.Tables[0];
                        foreach (DataRow dr in dt.Rows)
                        {
                            fnumresult.Text = dr["FamilyNum"].ToString();
                        }
                        successbox.Visible = true;
                        groupBox1.Visible = false;
                    }
                }
            }
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            HOME home = new HOME();
            home.homeEmpName = homeEmpName;
            home.homeEmpNum = homeEmpNum;
            home.Visible = true;
            this.Visible = false;
        }

        private void addmemberbutton_Click(object sender, EventArgs e)
        {
            ADDMEMBER am = new ADDMEMBER();
            am.homeEmpName = homeEmpName;
            am.homeEmpNum = homeEmpNum;
            am.familyNumber = int.Parse(fnumresult.Text);
            am.Visible = true;
            this.Visible = false;
        }
     
    }
}
